package com.ebupt.migu.music.effectStatistics.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ebupt.migu.music.common.auth.IDAuthUtils;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.util.EasyExcelUtil;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.effectStatistics.entity.vo.PUSHDailyReportExportVo;
import com.ebupt.migu.music.effectStatistics.entity.vo.PushExportVo;
import com.ebupt.migu.music.effectStatistics.enums.ExecuteType;
import com.ebupt.migu.music.effectStatistics.enums.SendChannel;
import com.ebupt.migu.music.effectStatistics.service.MessagePushService;

/**
 * @Author zhuar
 * 消息push
 */
@RestController
@RequestMapping("/MessagePushController")
public class MessagePushController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private IDAuthUtils idAuthUtils;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private MessagePushService messagePushService;

    /**
     * 分页、条件查询
     * 查询条件： 开始时间、结束时间、策略名称
     *
     * @return
     */
    @RequestMapping("/queryMap")
    public ResultObject queryMap() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        params = idAuthUtils.isAdmin(params);
        return messagePushService.queryMap(params);
    }

    /**
     * 查询条件： 开始时间、结束时间、策略名称
     * 全量导出
     *
     * @return
     */
    @RequestMapping(value = "/exportMap", produces = MediaType.TEXT_HTML_VALUE)
    public void exportMap() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        params = idAuthUtils.isAdmin(params);
        List<PushExportVo> list = messagePushService.exportMap(params);
        list.stream().map(exportVo -> {
            exportVo.setSendChannel(SendChannel.getNameByCode(exportVo.getSendChannel()));
            exportVo.setExecuteType(ExecuteType.getNameByCode(exportVo.getExecuteType()));
            ;
            return exportVo;
        }).collect(Collectors.toList());
        EasyExcelUtil.export(response, PushExportVo.class, list, "消息push");
    }

    /**
     * 分页、条件查询
     * 查询条件： 开始时间、结束时间、策略名称
     *
     * @return
     */
    @RequestMapping("/daily/report")
    public ResultObject report() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return messagePushService.report(params);
    }

    /**
     * 查询条件： 开始时间、结束时间、策略名称
     * 全量导出
     *
     * @return
     */
    @RequestMapping(value = "/daily/exportMap", produces = MediaType.TEXT_HTML_VALUE)
    public void dailyExportMap() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        List<PUSHDailyReportExportVo> list = messagePushService.dailyExportMap(params);
        EasyExcelUtil.export(response, PUSHDailyReportExportVo.class, list, "PUSH日报");
    }
}
